<?php
/*
 * Author:YangBo Created on 2009-1-22:锟斤拷锟斤拷07:50:10
 * 
 * TODO:TODO
 * as9
 * PhotoService
 * 
 */
require_once ("./BaseService.php");
require_once ("./interfaces/IPhotoService.php");
require_once ("./dao/Photo.php");
require_once ("./util/ServiceUtil.php");
class PhotoService extends BaseService implements IPhotoService {
	protected $photo;
	public function __construct() {
		$this->initializeDB ();
		$this->photo = new Photo ( );
	}
	/**
	 * 鍒涘缓鐩哥墖
	 * @param $target [pno,uploader,name,source,tag]
	 * @return:success
	 */
	public function create($target) {
		// 		$pno = strtolower(trim($target[0]));
		// 		$uploader = strtolower(trim($target[1]));
		// 		$name = strtolower(trim($target[2]));
		// 		$source = strtolower(trim($target[3]));
		// 		$tag = strtolower(trim($target[4]));
		// 		$ano = strtolower(trim($target[5]));
		

		$pno = $target ['pno'];
		$uploader = $target ['uploader'];
		$name = $target ['name'];
		$description = $target ['description'];
		$source = $target ['source'];
		$tag = $target ['tag'];
		$ano = $target ['ano'];
		
		try {
			$this->sql = "INSERT INTO photos(pno,uploader,name,description,source,tag,ano) VALUES('$pno','$uploader','$name','$description','$source','$tag','$ano')";
			$this->result = mysql_query ( $this->sql, $this->_db );
			
			try {
				$this->sql = "UPDATE albums SET albums.used=albums.used+1 WHERE albums.ano='$ano'";
				$this->result = mysql_query ( $this->sql, $this->_db );
				try {
					$this->sql = "UPDATE students SET students.uploaded=students.uploaded+1,students.messaged=students.messaged+1 WHERE students.email='$uploader'";
					$this->result = mysql_query ( $this->sql, $this->_db );
					
					return ServiceUtil::getSuccessResult ( "success", "create photo success!" );
				
				} catch ( Exception $exc ) {
					return $exc;
				}
			
			} catch ( Exception $exc ) {
				return $exc;
			}
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 璇诲彇鐩哥墖
	 * @param $targetID pno
	 * @return:$this->photo
	 */
	public function read($targetID) {
		if ($targetID) {
			$pno = strtolower ( trim ( $targetID ) );
			try {
				$this->sql = "SELECT * FROM photos WHERE pno='$pno'";
				$this->result = mysql_query ( $this->sql, $this->_db );
				
				while ( $row = mysql_fetch_object ( $this->result ) ) {
					$this->photo->pno = $row->pno;
					$this->photo->uploader = $row->uploader;
					$this->photo->name = $row->name;
					$this->photo->description = $row->description;
					$this->photo->source = $row->source;
					$this->photo->tag = $row->tag;
					$this->photo->avg_score = $row->avg_score;
					$this->photo->upload_time = $row->upload_time;
					$this->photo->ano = $row->ano;
					
					$this->ret [] = $this->photo;
				}
				return $this->ret;
			} catch ( Exception $exc ) {
				return $exc;
			}
		} else {
			try {
				$this->sql = "SELECT * FROM photos";
				$this->result = mysql_query ( $this->sql, $this->_db );
				$numRows = mysql_num_rows ( $this->result );
				for($i = 0; $i < $numRows; $i ++) {
					$row = mysql_fetch_object ( $this->result );
					$this->photo = new Photo ( );
					$this->photo->pno = $row->pno;
					$this->photo->uploader = $row->uploader;
					$this->photo->name = $row->name;
					$this->photo->description = $row->description;
					$this->photo->source = $row->source;
					$this->photo->tag = $row->tag;
					$this->photo->avg_score = $row->avg_score;
					$this->photo->upload_time = $row->upload_time;
					$this->photo->ano = $row->ano;
					
					$this->ret [] = $this->photo;
				}
				return $this->ret;
			} catch ( Exception $exc ) {
				return $exc;
			}
		}
	
	}
	/**
	 * 鏇存柊鐩哥墖
	 * @param $target [pno,uploader,name,source,tag]
	 * @return:success
	 */
	public function update($target) {
		$pno = strtolower ( trim ( $target [0] ) );
		$uploader = strtolower ( trim ( $target [1] ) );
		$name = strtolower ( trim ( $target [2] ) );
		$source = strtolower ( trim ( $target [3] ) );
		$tag = strtolower ( trim ( $target [4] ) );
		$avg_score = strtolower ( trim ( $target [5] ) );
		
		try {
			$this->sql = "UPDATE photos SET uploader='$uploader',name='$name',source='$source',tag='$tag',avg_score='$avg_score' WHERE pno='$pno'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			return ServiceUtil::getSuccessResult ( "success", "update photo success!" );
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 鍒犻櫎鐩哥墖
	 * @param $targetID pno
	 * @return:success
	 */
	public function delete($targetID) {
		$pno = strtolower ( trim ( $targetID ) );
		try {
			$this->sql = "DELETE FROM photos WHERE pno='$pno'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			return ServiceUtil::getSuccessResult ( "success", "delete photo success!" );
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 鐩哥墖鍒楄〃
	 * @param $targetID pno
	 * @return:success
	 */
	public function getPhotoList($albumID) {
		if ($albumID) {
			$ano = strtolower ( trim ( $albumID ) );
			try {
				$this->sql = "SELECT * FROM photos WHERE ano='$ano'";
				$this->result = mysql_query ( $this->sql, $this->_db );
				$numRows = mysql_num_rows ( $this->result );
				for($i = 0; $i < $numRows; $i ++) {
					$row = mysql_fetch_object ( $this->result );
					$this->photo = new Photo ( );
					$this->photo->pno = $row->pno;
					$this->photo->uploader = $row->uploader;
					$this->photo->name = $row->name;
					$this->photo->description = $row->description;
					$this->photo->source = $row->source;
					$this->photo->tag = $row->tag;
					$this->photo->upload_time = $row->upload_time;
					$this->photo->ano = $row->ano;
					$this->photo->avg_score = $row->avg_score;
					$this->photo->capcity = $row->capcity;
					
					$this->ret [] = $this->photo;
				}
				try {
					$this->sql = "UPDATE albums SET albums.browsed=albums.browsed+1 WHERE albums.ano='$ano'";
					$this->result = mysql_query ( $this->sql, $this->_db );
					return $this->ret;
				} catch ( Exception $exc ) {
					return $exc;
				}
			} catch ( Exception $exc ) {
				return $exc;
			}
		}
	}
	/**
	 * 鏍囩鍚嶆悳绱�
	 * @param $tag
	 * @return:array
	 */
	public function findPhotosByTag($tag) {
		$myTag = strtolower ( trim ( $tag ) );
		try {
			$this->sql = "SELECT * FROM photos WHERE tag LIKE'%$myTag%'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			$numRows = mysql_num_rows ( $this->result );
			for($i = 0; $i < $numRows; $i ++) {
				$row = mysql_fetch_object ( $this->result );
				$this->photo = new Photo ( );
				$this->photo->pno = $row->pno;
				$this->photo->uploader = $row->uploader;
				$this->photo->name = $row->name;
				$this->photo->description = $row->description;
				$this->photo->source = $row->source;
				$this->photo->tag = $row->tag;
				$this->photo->upload_time = $row->upload_time;
				$this->photo->ano = $row->ano;
				$this->photo->avg_score = $row->avg_score;
				$tihs->photo->capcity = $row->capcity;
				
				$this->ret [] = $this->photo;
			}
			return $this->ret;
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 鍥剧墖鍚嶆悳绱�
	 * @param $name
	 * @return:array
	 */
	public function findPhotosByName($name) {
		$myName = strtolower ( trim ( $name ) );
		try {
			$this->sql = "SELECT * FROM photos WHERE name LIKE'%$myName%'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			$numRows = mysql_num_rows ( $this->result );
			for($i = 0; $i < $numRows; $i ++) {
				$row = mysql_fetch_object ( $this->result );
				$this->photo = new Photo ( );
				$this->photo->pno = $row->pno;
				$this->photo->uploader = $row->uploader;
				$this->photo->name = $row->name;
				$this->photo->description = $row->description;
				$this->photo->source = $row->source;
				$this->photo->tag = $row->tag;
				$this->photo->upload_time = $row->upload_time;
				$this->photo->ano = $row->ano;
				$this->photo->avg_score = $row->avg_score;
				$tihs->photo->capcity = $row->capcity;
				
				$this->ret [] = $this->photo;
			}
			return $this->ret;
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 鍥剧墖鐣欒█閲�
	 * @param $pno pno
	 * @return:int
	 */
	public function getMessageLeft($pno) {
		$myPno = strtolower ( trim ( $pno ) );
		try {
			$this->sql = "SELECT message.pno FROM message WHERE pno='$myPno'";
			$this->result = mysql_query ( $this->sql, $this->_db );
			$numRows = mysql_num_rows ( $this->result );
			return $numRows;
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	/**
	 * 鐩哥墖鍒楄〃	 
	 * @return:Array
	 */
	public function getThumbnailList() {
		try {
			$this->sql = "SELECT photos.source FROM photos";
			$this->result = mysql_query ( $this->sql, $this->_db );
			$numRows = mysql_num_rows ( $this->result );
			for($i = 0; $i < $numRows; $i ++) {
				$row = mysql_fetch_object ( $this->result );
				$this->photo = new Photo ( );
				$tn = "tn";
				list($prefix,$sign,$category,$basefile) = explode ("/",$row->source);
				$tnSource = $prefix."/".$sign."/".$category."/".$tn."/".trim($basefile);
				$this->photo->thumbnail = $tnSource;
				$this->ret[] = $this->photo;
			}
			return $this->ret;
		} catch ( Exception $exc ) {
			return $exc;
		}
	}
	public function __destruct() {
	
	}
}
?>
